//
// ColumnType
//

package net.fauxjobuilder;

import java.sql.*;

public enum ColumnType
{
    Short( "Short",
    "short",
    "setShort",
    Types.SMALLINT ),

    Integer( "Integer",
    "int",
    "setInt",
    Types.INTEGER ),

    Long( "Long",
    "long",
    "setLong",
    Types.BIGINT ),

    Double( "Double",
    "double",
    "setDouble",
    Types.DOUBLE ),

    String( "String",
    "String",
    "setString",
    Types.VARCHAR ),

    Char( "String",
    "String",
    "setString",
    Types.CHAR ),

    Boolean( "Boolean",
    "boolean",
    "setBoolean",
    Types.BIT,
    Types.BOOLEAN ),

    Date( "java.util.Date",
    "java.util.Date",
    "setObject",
    Types.DATE ),

    Timestamp( "java.util.Date",
    "java.util.Date",
    "setObject",
    Types.TIMESTAMP ),

    Binary( "byte[]",
    "byte[]",
    "setBytes",
    Types.BINARY ),

    BigDecimal( "java.math.BigDecimal",
    "java.math.BigDecimal",
    "setBigDecimal",
    Types.NUMERIC );

    private int[] _dbTypes;
    private String _nullableType;
    private String _notNullableType;
    private String _statementSetter;

    private ColumnType( String nullableType, String notNullableType, String setter, int ...dbTypes )
    {
        _nullableType = nullableType;
        _notNullableType = notNullableType;
        _dbTypes = dbTypes;
        _statementSetter = setter;
    }

    public static ColumnType find( int findDBType )
    {
        for ( ColumnType type : ColumnType.values() )
        {
            for ( int dbType : type._dbTypes )
            {
                if ( dbType == findDBType )
                {
                    return type;
                }
            }
        }

        return null;
    }

    public String nullableType()
    {
        return _nullableType;
    }

    public String notNullableType()
    {
        return _notNullableType;
    }

    public String statementSetter()
    {
        return _statementSetter;
    }

    public String toString( boolean nullable )
    {
        return nullable ? _nullableType : _notNullableType;
    }
}

